package cloud.seri.iot.aliyun

import com.aliyuncs.DefaultAcsClient
import com.aliyuncs.iot.model.v20180120.*

class ProductManager(private val client: DefaultAcsClient)
{

    /**
     * 创建产品
     *
     * @param ProductName         产品名称  必须
     * @param NodeType            节点类型  必须
     * @param AliyunCommodityCode 产品版本类型
     * @param DataFormat          产品数据格式
     * @param CategoryId          设备类型
     * @param Description
     * @param ProtocolType
     * @return 产品创建信息
     */
    fun createProduct(ProductName: String,
                      NodeType: Int? = null,
                      AliyunCommodityCode: String? = "iothub_senior",
                      DataFormat: Int? = 0,
                      CategoryId: Long? = null,
                      Description: String? = null,
                      ProtocolType: String? = null,
                      NetType: String? = "WIFI") = client.getAcsResponse(CreateProductRequest().apply {
        productName = ProductName
        nodeType = NodeType
        aliyunCommodityCode = AliyunCommodityCode
        dataFormat = DataFormat
        categoryId = CategoryId
        description = Description
        protocolType = ProtocolType
        netType = NetType
    })

    /**
     * 修改产品
     *
     * @param ProductKey  产品PK 必需
     * @param ProductName 产品名称 非必需
     * @param Description 产品描述 非必需
     */
    fun updateProduct(ProductKey: String, ProductName: String, Description: String? = null) = client.getAcsResponse(UpdateProductRequest().apply {
        productKey = ProductKey
        productName = ProductName
        description = Description
    })

    /**
     * 查询产品列表
     *
     * @param CurrentPage         指定从返回结果中的第几页开始显示  非必需
     * @param PageSize            指定返回结果中每页显示的记录数量，最大值是200 非必需
     * @param AliyunCommodityCode 产品描述 非必需
     */
    fun QueryProductList(CurrentPage: Int, PageSize: Int,
                         AliyunCommodityCode: String? = null) = client.getAcsResponse(QueryProductListRequest().apply {
        aliyunCommodityCode = AliyunCommodityCode
        pageSize = PageSize
        currentPage = CurrentPage
    })

    /**
     * 查询产品详细信息
     *
     * @param ProductKey 产品PK 必需
     */
    fun QueryProduct(ProductKey: String) = client.getAcsResponse(QueryProductRequest().apply {
        productKey = ProductKey
    })

    /**
     * 删除产品
     *
     * @param ProductKey 产品PK 必需
     */
    fun DeleteProduct(ProductKey: String) = client.getAcsResponse(DeleteProductRequest().apply {
        productKey = ProductKey
    })
}

